﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace CMS
{
    public partial class findscore : System.Web.UI.Page
    {   
        public FenYe fenye = new FenYe();

        string url = "findscore.aspx";

        protected void Page_Load(object sender, EventArgs e)
        {
            //获取用户名
            if (Session["stu_no"] != null)
            {
                this.stu_no.InnerText = Session["stu_no"].ToString();
            }
            //else
            //{
            //    Response.Redirect("log.aspx");
            //}
            if (Session["stu_name"] != null)
            {
                this.stu_name.InnerText = Session["stu_name"].ToString();
            }

            if (Request.QueryString["pageNow"] != null)
            {
                fenye.pageNow = int.Parse(Request.QueryString["pageNow"]);
                this.showTh.Visible = true;
                this.data_out();
            }

        }

        protected void score_query_Click(object sender, EventArgs e)
        {
            this.showTh.Visible = true;
            this.data_out();
        }

        public void data_out() {
            //Response.Write("<script>alert('我很饿')</script>");

            SqlDataReader dr = null;
           
            SqlHelper sqlHelper = new SqlHelper();

            SqlParameter[] pms = null;

      
            string pageNow = fenye.pageNow.ToString();
            string pageSize = fenye.pageSize.ToString();
            string notFound = ((fenye.pageNow - 1) * fenye.pageSize).ToString();

            //查询总记录数
            StringBuilder sb1 = new StringBuilder(1024);
            sb1.Append("SELECT   COUNT(*) AS '总记录数' ");
            sb1.Append("FROM      Course INNER JOIN ");
            sb1.Append("                Stu_Score ON Course.Cno = Stu_Score.Cno INNER JOIN ");
            sb1.Append("                student ON Stu_Score.StuNo = student.StuNo INNER JOIN ");
            sb1.Append("                Teacher ON Stu_Score.Tno = Teacher.Tno ");
            sb1.Append("WHERE   (student.StuNo = @StuNo) AND (Stu_Score.Grade = @Grade) AND (Stu_Score.Term = @Term) ");


            StringBuilder sb2 = new StringBuilder(1024);
            sb2.AppendFormat("SELECT   TOP ({0}) Stu_Score.Id, Stu_Score.Grade, Stu_Score.Term, Course.Cname, Stu_Score.Score ", pageSize);
            sb2.Append("FROM      Course INNER JOIN ");
            sb2.Append("                Stu_Score ON Course.Cno = Stu_Score.Cno INNER JOIN ");
            sb2.Append("                student ON Stu_Score.StuNo = student.StuNo INNER JOIN ");
            sb2.Append("                Teacher ON Stu_Score.Tno = Teacher.Tno ");
            sb2.Append("WHERE   (student.StuNo = @StuNo) AND (Stu_Score.Grade = @Grade) AND (Stu_Score.Term = @Term) AND  ");
            sb2.Append("                (Stu_Score.Id NOT IN ");
            sb2.AppendFormat("                    (SELECT   TOP ({0}) Stu_Score_1.Id ", notFound);
            sb2.Append("                     FROM      Course AS Course_1 INNER JOIN ");
            sb2.Append("                                     Stu_Score AS Stu_Score_1 ON Course_1.Cno = Stu_Score_1.Cno INNER JOIN ");
            sb2.Append("                                     student AS student_1 ON Stu_Score_1.StuNo = student_1.StuNo INNER JOIN ");
            sb2.Append("                                     Teacher AS Teacher_1 ON Stu_Score_1.Tno = Teacher_1.Tno ");
            sb2.Append("                     WHERE   (student_1.StuNo = @StuNo) AND (Stu_Score_1.Grade = @Grade) AND (Stu_Score_1.Term = @Term))) ");

            pms = new SqlParameter[3];
            pms[0] = new SqlParameter("@StuNo", Session["stu_no"].ToString());
            pms[1] = new SqlParameter("@Grade", int.Parse(ddlgrade.Text));
            pms[2] = new SqlParameter("@Term", int.Parse(ddlterm.Text));

            sqlHelper.fenye_query(sb1.ToString(), sb2.ToString(), url, fenye, pms);

            //Response.Write(fenye.rowCount);
            //Response.Write(fenye.pageCount);


            //dr = fenye.dr;
            //while (dr.Read())
            //{
            //    Response.Write(" " + dr["Cname"].ToString() + " " + dr["Score"].ToString() + " " + dr["Grade"].ToString() + " " + dr["Term"].ToString() + "<br/>");
            //}

            //Response.Write(fenye.navigate);

        }

        
    }
}